package com.example.com.daervideo.vm;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.Toast;

import com.example.com.daervideo.activitys.loginRegist.LoginActivity;
import com.example.com.daervideo.activitys.users.SettingActivity;
import com.example.com.daervideo.activitys.loginRegist.ForgetAndResetPassword;
import com.example.com.daervideo.utils.CacheUtil;

import me.goldze.mvvmhabit.base.AppManager;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.SPUtils;

/**
 * Created by i7-1 on 2018/12/6.
 */

public class SettingViewModel extends BaseViewModel {
    private final SettingActivity settingActivity;

    public SettingViewModel(SettingActivity settingActivity) {
        super(settingActivity);
        this.settingActivity = settingActivity;
    }

    public BindingCommand resetPasword = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            startActivity(ForgetAndResetPassword.class);
        }
    });
    public BindingCommand clearCache = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            CacheUtil.clearAllCache(context);
            settingActivity.getBinding().cache.setText("0.0");
            Toast.makeText(context, "缓存已清理", Toast.LENGTH_SHORT).show();
        }
    });
    public BindingCommand checkVersion = new BindingCommand(new BindingAction() {
        @Override
        public void call() {

            Toast.makeText(context, "检查版本", Toast.LENGTH_SHORT).show();
        }
    });

    public BindingCommand loginout = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            Log.e("Setting", "Loginout");
            Class<LoginActivity> aClass = LoginActivity.class;
            SPUtils.getInstance().put("isFirst", true);
            startActivity(aClass);
            AppManager.getAppManager().finishOtherActivityDelay(aClass);
        }
    });


    @Override
    public void onCreate() {
        super.onCreate();
        try {
            settingActivity.getBinding().cache.setText(CacheUtil.getTotalCacheSize(context));
            settingActivity.getBinding().version.setText(getVersionName());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private String getVersionName() throws Exception {
        // 获取packagemanager的实例
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()是你当前类的包名，0代表是获取版本信息
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        String version = packInfo.versionName;
        return version;
    }
}
